package com.mlnx.web.ui.controller;

import com.mlnx.common.entity.Response;
import com.mlnx.data.entity.SugarInfo;
import com.mlnx.data.pojo.vo.SugarDayVo;
import com.mlnx.data.service.SugarDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.shan.spring.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Api(value = "UI血糖接口", tags = {"UI血糖接口"})
@RestController
@RequestMapping("ui/bg")
public class BgUiController extends BaseController {
    @Autowired
    private SugarDataService sugarDataService;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");


    @ApiOperation("获取血糖表格统计数据")
    @GetMapping(value = "/table")
    public Response getTableBgData(@RequestParam Integer patientId, @RequestParam Long startTime, @RequestParam Long endTime) {
        List<SugarInfo> sugars = sugarDataService.getSugars(patientId, startTime, endTime);
        if (sugars == null || sugars.size() <= 0)
            return result();

        Map<String, SugarDayVo> map = new HashMap<>();
        for (SugarInfo sugar : sugars) {
            String day = simpleDateFormat.format(new Date(sugar.getTime()));
            SugarDayVo sugarDayVo;
            if (map.containsKey(day))
                sugarDayVo = map.get(day);
            else
                sugarDayVo = new SugarDayVo();

            switch (sugar.getState()) {
                case "1":
                    sugarDayVo.setBeforeBreakfast(sugar.getResult());
                    break;
                case "2":
                    sugarDayVo.setAfterBreakfast(sugar.getResult());
                    break;
                case "4":
                    sugarDayVo.setEmpty(sugar.getResult());
                    break;
                case "8":
                    sugarDayVo.setDawn(sugar.getResult());
                    break;
                default:
                    break;
            }

            if (!map.containsKey(day))
                map.put(day, sugarDayVo);
        }

        List<SugarDayVo> sugarDayVos = new ArrayList<>();
        for (String day : map.keySet()) {
            try {
                map.get(day).setDay(simpleDateFormat.parse(day).getTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }

            sugarDayVos.add(map.get(day));
        }

        Collections.sort(sugarDayVos);
        return result(sugarDayVos);
    }

    @ApiOperation("获取血糖趋势统计数据")
    @GetMapping(value = "/trend")
    public Response getTrendBgData(@RequestParam Integer patientId, @RequestParam Long
            startTime, @RequestParam Long endTime) {
        List<SugarInfo> sugars = sugarDataService.getSugars(patientId, startTime, endTime);
        if (sugars == null || sugars.size() <= 0)
            return result();

        Map<String, List<SugarInfo>> map = new HashMap<>();
        for (SugarInfo sugar : sugars) {
            String state = sugar.getState();
            if (map.containsKey(state))
                map.get(state).add(sugar);
            else {
                List<SugarInfo> sugarInfos = new ArrayList<>();
                sugarInfos.add(sugar);
                map.put(state, sugarInfos);
            }
        }

        return result(map);
    }
}

